// 配置 store 
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
import createPersistedState from "vuex-persistedstate"

// 通过 Vue.use() 安装 Vuex
Vue.use(Vuex);

const store = new Vuex.Store({
    // state存数据
    state: {
        nickname: '',
        photoImg: '',
        loginToken: '',
        editId: '',
        userId: '',
        phoneNum: '',
        cerateTime: '',
        registryProvince: '',
        registryCity: '',
        orderId: '',
        goodsReturnId: '',
        authorityId: '',
    },
    // vue更新store中数据的唯一方式是提交mutation
    // mutation:类似于事件,有一个事件类型和一个事件操作函数,操作函数的第一个参数是state对象
    mutations: {
        nickname: function (state, data) {
            state.nickname = data
        },
        photoImg: function (state, data) {
            state.photoImg = data
        },
        loginToken: function (state, data) {
            state.loginToken = data
        },
        editId: function (state, data) {
            state.editId = data
        },
        userId: function (state, data) {
            state.userId = data
        },
        phoneNum: function (state, data) {
            state.phoneNum = data
        },
        cerateTime: function (state, data) {
            state.cerateTime = data
        },
        registryProvince: function (state, data) {
            state.registryProvince = data
        },
        registryCity: function (state, data) {
            state.registryCity = data
        },
        orderId: function (state, data) {
            state.orderId = data
        },
        goodsReturnId: function (state, data) {
            state.goodsReturnId = data
        },
        authorityId: function (state, data) {
            state.authorityId = data
        },
    },
    // store中异步操作
    actions: {
        // context:上下文,代表当前state对象
        // getData: function (context, type) {
        //     axios.get(`/api/api/rank?cate=${this.state.cate}&type=${type}&date=${this.state.time}`)
        //         .then(function (data) {
        //             // 更新state中数据,必须提交mutation
        //             let code = data.data.code;
        //             if (code !== 0) {
        //                 this.state.lists = '';
        //             } else {
        //                 context.commit('lists', data.data.data.data.filter(i => i.name))
        //                 context.commit('createdAts', data.data.data)
        //             }
        //         }.bind(this))
        //         .catch(function (err) {
        //             console.log(err);
        //         })
        // },
    },
    plugins: [createPersistedState()]
});

export default store;